package com.example.healthapp.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.healthapp.R;
import com.example.healthapp.utils.InterfaceTool;
import com.example.healthapp.utils.SPManage;

// 新增导入数据库相关
import kt_test.room.database.OxygenDatabase;
import kt_test.room.entity.UserInfoEntity;

public class RegisterActivity extends AppCompatActivity {

    EditText register_account, register_password, register_user_id, register_gender, register_age;
    TextView register_rgt;
    ImageView register_back;

    private OxygenDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        InterfaceTool.setFullScreen(this, false);
        initview();
        db = OxygenDatabase.Companion.getDatabase(this); // ⚡ 正确初始化
        event();
    }

    private void initview() {
        register_account = findViewById(R.id.register_account);
        register_password = findViewById(R.id.register_password);
        register_user_id = findViewById(R.id.register_user_id);
        register_gender = findViewById(R.id.register_gender);
        register_age = findViewById(R.id.register_age);
        register_rgt = findViewById(R.id.register_rgt);
        register_back = findViewById(R.id.register_back);
    }

    private void event() {
        register_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        register_rgt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String account = register_account.getText().toString();
                String password = register_password.getText().toString();
                String userId = register_user_id.getText().toString();
                String gender = register_gender.getText().toString();
                String age = register_age.getText().toString();

                if (account.equals("") || password.equals("") || userId.equals("") || gender.equals("") || age.equals("")) {
                    Toast.makeText(RegisterActivity.this, "账号、密码、用户ID、性别、年龄都不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    // 保存到 SharedPreferences
                    SPManage.getSP(RegisterActivity.this, "account").putString("account", account);
                    SPManage.getSP(RegisterActivity.this, "account").putString("password", password);
                    SPManage.getSP(RegisterActivity.this, "account").putString("userId", userId);
                    SPManage.getSP(RegisterActivity.this, "account").putString("gender", gender);
                    SPManage.getSP(RegisterActivity.this, "account").putString("age", age);
                    SPManage.getSP(RegisterActivity.this, "account").commit();

                    // 保存到 Room数据库（子线程）
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            UserInfoEntity userInfoEntity = new UserInfoEntity(
                                    userId,
                                    gender,
                                    Integer.parseInt(age)
                            );
                            db.userInfoDao().insertDirect(userInfoEntity);
                        }
                    }).start();

                    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                    finish();
                }
            }
        });
    }
}
